<html style="margin:0px;overflow:hidden;">
<head>
  <title id="titleID">流程图</title>
  <link href="editor/favicon.ico" type="image/x-png" rel="shortcut icon">
  <script type="text/javascript" src="https://cdn.staticfile.org/pako/1.0.10/pako.min.js"></script>
  <script src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js"></script>
  <script src="diagram-editor.js"></script>
  <script type="text/javascript">
    var graph = null;
    var golobalThis = this;
    function getParams(key) {
        let search = window.location.search.replace(/^\?/, "");
        let pairs = search.split("&");
        let paramsMap = pairs.map(pair => {
            let [key, value] = pair.split("=");
            return [decodeURIComponent(key), decodeURIComponent(value)];
        }).reduce((res, [key, value]) => Object.assign(res, { [key]: value }), {});
        return paramsMap[key] || "";
    }
    var xml = "";
    var md_url = "http://localhost:1323"
    var type =getParams("type")
    var param = { 
      file: getParams("file"),
      id: getParams("id"),
      type: getParams("type")
    }
    let paramSplis = param.file.split("/");
    $('#titleID').text(paramSplis[paramSplis.length-1]);
    // console.log(param)
    $.ajax({
       type: "GET",
       url: `${md_url}/md`,
       data: param,
       // dataType: "json",
       async: false,
       success: function(data){
         // console.log(data)
         try{
          let content = $(data).find("diagram");
          if(content != null){
            data = decode(content.text());
          }
        }catch(e){
          
        }
        console.log("Data Loaded: " + data);
        // 处理id
        // let xml2 = $.parseXML(data);
        // xml2.
        xml = data;

       }
    });

  function mxClientOnLoad(stylesheet)
    {
      // xml = document.getElementById('mxfile').innerHTML;
      // xml = document.getElementById('mxfile').innerHTML;
      xml = encodeURIComponent(xml);
      xml = decodeURIComponent(xml);

      // Removes all illegal control characters before parsing
      var checked = [];

      for (var i = 0; i < xml.length; i++)
      {
        var code = xml.charCodeAt(i);

        // Removes all control chars except TAB, LF and CR
        if (code >= 32 || code == 9 || code == 10 || code == 13)
          {
            checked.push(xml.charAt(i));
          }
      }

      xml = checked.join('');

      var div = document.createElement('div');
      div.style.width = '100%';
      div.style.height = '100%';
      div.style.position = 'relative';
      document.body.appendChild(div);
      graph = new mxGraph(div);

      graph.resetViewOnRootChange = false;
      graph.foldingEnabled = false;
      // NOTE: Tooltips require CSS
      graph.setTooltips(false);
      graph.setEnabled(false);

      // Loads the stylesheet
      if (stylesheet != null)
      {
        var xmlDoc = mxUtils.parseXml(stylesheet);
        var dec = new mxCodec(xmlDoc);
        dec.decode(xmlDoc.documentElement, graph.getStylesheet());
      }

      var xmlDoc = mxUtils.parseXml(xml);
      var codec = new mxCodec(xmlDoc);
      codec.decode(codec.document.documentElement, graph.getModel());
      graph.maxFitScale = 1;
      graph.dialect = 'strictHtml';
      // debugger
      graph.fit();
      graph.center(true, false);

      window.addEventListener('resize', function()
      {
        graph.fit();
        graph.center(true, false);
      });


    }

      // if(type == 'drawio'){
      //   $(function(){
      //     // edit()
      //     DiagramEditor.editElement(xml)
      //   })
        
      // }


    


function decode(data) {

        // data = atob($content.text());
        data = atob(data);
        data = bytesToString(pako.inflateRaw(data));
        data = decodeURIComponent(data);
        // Removes all illegal control characters before parsing
        var checked = [];

        for (var i = 0; i < data.length; i++) {
            var code = data.charCodeAt(i);

            // Removes all control chars except TAB, LF and CR
            if (code >= 32 || code == 9 || code == 10 || code == 13) {
                checked.push(data.charAt(i));
            }
        }

        data = checked.join('');

        return data;
    };

    function bytesToString(arr) {
        var str = '';

        for (var i = 0; i < arr.length; i++) {
            str += String.fromCharCode(arr[i]);
        }

        return str;
    };

  </script>
</head>
<body style="margin:10px;overflow:hidden;">

<script type="text/javascript">
var doc = document.documentElement.outerHTML;
</script>
<script type="text/javascript" src="http://localhost:1313/drawio/embed.js"></script></body></html>